package cn.sail.spring.aop.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理处理类型
 * 告诉jdk 怎么区创建代理类型
 */
public class ProxyHandler implements InvocationHandler {
    //目标对象
    private Object target;

    public ProxyHandler() {
    }

    public ProxyHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //todo 开启事务
        System.out.println("开启事务");

        Object result = null;
        try {
            //调用目标对象的业务方法
            result = method.invoke(target,args);
        }catch (Exception e){
            System.out.println("事务回滚");
            return null;
        }

        System.out.println("事务提交");
        return result;
    }
}
